package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ProvinceBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.my.AddBankCardPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.RxTimerUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.StringUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IAddBankCardView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/6/11.
 * Desc   :
 */

public class AddBankCardActivity extends BaseActivity<AddBankCardPresenter> implements IAddBankCardView {

    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.view_bottomline)
    View viewBottomline;
    @BindView(R.id.txt_province)
    TextView txtProvince;
    @BindView(R.id.txt_city)
    TextView txtCity;
    @BindView(R.id.txt_bank_name)
    TextView txtBankName;
    @BindView(R.id.txt_bank_address)
    TextView txtBankAddress;
    @BindView(R.id.txt_send_code)
    TextView txtSendCode;
    @BindView(R.id.et_bankcard_num)
    EditText etBankcardNum;
    @BindView(R.id.txt_bankcard_ownername)
    EditText txtBankcardOwnername;
    @BindView(R.id.et_phone_number)
    EditText etPhoneNumber;
    @BindView(R.id.et_yanzheng)
    EditText etYanzheng;

    private String p_id;
    private String ca_name, ca_phone, ca_number, city1, city2, token, code, ca_ress, ca_wxbanklist;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_add_bankcard);
    }

    @Override
    protected void initPresenter() {
        mPresenter = new AddBankCardPresenter(this);
    }

    @Override
    protected void init() {
        txtTitle.setText("绑定银行卡");
        token = SharedPreferencesUtil.getsInstances(this).getLoginInfo().getToken();
    }

    @OnClick({R.id.ll_leftview, R.id.ll_province, R.id.ll_city, R.id.ll_bank_name, R.id
            .ll_bank_address, R.id.txt_send_code, R.id.txt_confirm_band})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.ll_province:
                mPresenter.getProvince("");
                break;
            case R.id.ll_city:
                if (StringUtils.isEmpty(p_id)) {
                    mPresenter.getCity(p_id);
                } else {
                    showTip("请选择省份");
                }
                break;
            case R.id.ll_bank_name:
                mPresenter.getBank();
                break;
            case R.id.ll_bank_address:
                InputBankAdressDialog inputBankAdressDialog = new InputBankAdressDialog();
                inputBankAdressDialog.setInputBankAddressListener(bankAddress -> {
                    ca_ress = bankAddress;
                    txtBankAddress.setText(bankAddress);
                });
                inputBankAdressDialog.show(getSupportFragmentManager(),"InputBankAdressDialog");
                break;
            case R.id.txt_send_code:
                String phoneNumber = etPhoneNumber.getText().toString();
                if (StringUtils.isEmpty(phoneNumber)) {
                    mPresenter.sendMsg(phoneNumber, "3");
                    RxTimerUtil.interval(1, 60, new RxTimerUtil.IRxNext() {
                        @Override
                        public void doNext(long number) {
                            txtSendCode.setClickable(false);
                            txtSendCode.setText(String.format(ResourcesUtils.getString(R.string.repeat_verify_code_hint), 60 - number));
                        }

                        @Override
                        public void doComplete() {
                            txtSendCode.setClickable(true);
                            txtSendCode.setText("获取验证码");
                            txtSendCode.setTextColor(ResourcesUtils.getColor(R.color.white));
                        }
                    });
                } else {
                    showTip("请输入手机号");
                }
                break;
            case R.id.txt_confirm_band:
                ca_name = txtBankcardOwnername.getText().toString();
                ca_phone = etPhoneNumber.getText().toString();
                ca_number = etBankcardNum.getText().toString();
//                city1 = txtProvince.getText().toString();
//                city2 = txtCity.getText().toString();
                code = etYanzheng.getText().toString();
//                ca_ress = txtBankAddress.getText().toString();
//                ca_wxbanklist = txtBankName.getText().toString();

                if (StringUtils.isEmpty(city1)) {
                    if (StringUtils.isEmpty(city2)) {
                        if (StringUtils.isEmpty(ca_wxbanklist)) {
                            if (StringUtils.isEmpty(ca_ress)) {
                                if (StringUtils.isEmpty(ca_number)) {
                                    if (StringUtils.isEmpty(ca_name)) {
                                        if (StringUtils.isEmpty(ca_phone)) {
                                            if (StringUtils.isEmpty(code)) {
                                                mPresenter.addCard(ca_name, ca_phone, ca_number, city1, city2, token, code, ca_ress, ca_wxbanklist);
                                            } else {
                                                showTip("请输入验证码");
                                            }
                                        } else {
                                            showTip("请输入手机号");
                                        }
                                    } else {
                                        showTip("请输入持卡人姓名");
                                    }
                                } else {
                                    showTip("请输入银行卡号");
                                }
                            } else {
                                showTip("请选择开户网点");
                            }
                        } else {
                            showTip("请选择银行");
                        }
                    } else {
                        showTip("请选择城市");
                    }
                } else {
                    showTip("请选择省份");
                }
                break;
        }
    }

    @Override
    public void getProvince(List<ProvinceBean> provinceBeans) {
        ChooseDialog chooseDialog = ChooseDialog.newInstance("请选择省份");
        chooseDialog.setData(provinceBeans);
        chooseDialog.setPIDListener(new ChooseDialog.PIDListener() {
            @Override
            public void getPid(ProvinceBean pid) {
                p_id = pid.getCy_id();
                city1 = pid.getCy_name();
                txtProvince.setText(pid.getCy_name());
                txtCity.setText("开户行所在城市");
            }

            @Override
            public void getBank(String bankName) {
                ca_wxbanklist = bankName;
            }
        });
        chooseDialog.show(getSupportFragmentManager(),"choose_province");
    }

    @Override
    public void getCity(List<ProvinceBean> provinceBeans) {
        ChooseDialog chooseDialog = ChooseDialog.newInstance("请选择城市");
        chooseDialog.setData(provinceBeans);
        chooseDialog.setPIDListener(new ChooseDialog.PIDListener() {
            @Override
            public void getPid(ProvinceBean pid) {
                city2 = pid.getCy_name();
                txtCity.setText(pid.getCy_name());
            }

            @Override
            public void getBank(String bankName) {

            }
        });
        chooseDialog.show(getSupportFragmentManager(),"choose_city");
    }

    @Override
    public void getBank(List<String> bankList) {
        ChooseDialog chooseDialog = ChooseDialog.newInstance("请选择银行");
        chooseDialog.setBankData(bankList);
        chooseDialog.setPIDListener(new ChooseDialog.PIDListener() {
            @Override
            public void getPid(ProvinceBean pid) {

            }

            @Override
            public void getBank(String bankName) {
                ca_wxbanklist = "农业";
                txtBankName.setText(bankName);
            }
        });
        chooseDialog.show(getSupportFragmentManager(),"choose_bank");
    }

    @Override
    public void addCard(String result) {
        showTip(result);
        if (result.contains("添加成功")) {
            finish();
        }
    }
}
